package no.ntnu.fp.gui.notice;

import java.awt.Font;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import no.ntnu.fp.gui.mainFrame.ModelListenerProvider;
import no.ntnu.fp.model.Notice;
import no.ntnu.fp.model.events.ModelEvent;
import no.ntnu.fp.model.events.NoticesChangedEvent;
import no.ntnu.fp.model.events.NoticesClearedEvent;
import no.ntnu.fp.model.listeners.ModelListener;

@SuppressWarnings("serial")
public class NoticeBoard extends JPanel implements ModelListenerProvider{
	private NoticeScrollPanel noticeScrollPanel;
	
	public NoticeBoard(){
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JLabel noticeLabel = new JLabel("Varsler:", JLabel.CENTER);
		noticeLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		noticeScrollPanel = new NoticeScrollPanel();
		JButton showArchivedNotices = new JButton("Vis gamle varsler");
		
		this.add(noticeLabel);
		this.add(noticeScrollPanel);
		this.add(showArchivedNotices);
		this.setSize(250, 400);
	}
	
	public void addNotice(Notice notice){
		noticeScrollPanel.addNotice(notice);
	}
	
	private final class NoticeChangeListener implements ModelListener<NoticesChangedEvent>{
		@Override
		public void handleEvent(NoticesChangedEvent event) {
			noticeScrollPanel.clearPanel();
			noticeScrollPanel.validate();
			for(Notice n : event.getNewNotices()){
				addNotice(n);
			}
		}
	}

	private final class NoticesClearedListener implements ModelListener<NoticesClearedEvent> {
		@Override
		public void handleEvent(NoticesClearedEvent event) {
			noticeScrollPanel.clearPanel();
		}
	}
	
	@Override
	public Map<Class<? extends ModelEvent>, ModelListener<?>> getEventListeners() {
		Map<Class<? extends ModelEvent>, ModelListener<?>> map = 
			new HashMap<Class<? extends ModelEvent>, ModelListener<?>>();
		map.put(NoticesChangedEvent.class, new NoticeChangeListener());
		map.put(NoticesClearedEvent.class, new NoticesClearedListener());
		return map;
	}

}
